<?php
require_once 'ApibaseController.php';

/**
 * 友の会用フォーム・問い合わせAPI<br/>
 *
 * @package apamanshop
 * @author H.Imamura
 * @copyright (c) 2013 SystemSoft Corp. All rights reserved
 */
class Inquiry_TomonokaisendController extends Inquiry_ApibaseController
{
	private $_tomonokai_mail = 'info-apamantomonokai@apamanshop.co.jp';

	/**
	 * 15週連続キャンペーン送信処理
	 *
	 * @access public
	 * @return void
	 */
	public function anniversary15Action()
	{
		// 変数初期化
		$api_params = array();
		$api_params['TimeStr']   = $this->GetTimeStringDisp();	// 送信時刻
		$api_params['ApplyDate'] = date('YmdHis');
		
		$api_params['CampaignCd'] = '1312_anniversary';
		$api_params['CampaignName'] = '15週連続キャンペーン';
		$api_params['LoginId']    = $this->_params['Number'];
		$api_params['Email']      = $this->_params['Mail'];
		$api_params['MemberId']   = '';
		$api_params['MemberName'] = '';
		
		$api_params['MAIL_TOMONOKAI'] = $this->_tomonokai_mail;
		
		//--- メール判定 ---
		$this->setSendMailFlag($api_params['Email']);
		
		// モデルの呼び出し
		$this->_model = new Inquiry_Model_Tomonokai();
		$db = $this->_model->getDbConnection();
		
		// メンバーID検索
		$member_data = $this->_model->selectMemberIdForLoginId($api_params, $db);
		if (!empty($member_data)) {
			$api_params['MemberId']   = $member_data['MEMBER_ID'];
			$api_params['MemberName'] = $member_data['MEMBER_NAME'];
		}
		if (empty($api_params['MemberId'])) {
			$db->closeConnection();
			
			// リダイレクトパラメータ設定
			$re_params = array();
			$re_params['error'] = '1';
			$mySession = new Zend_Session_Namespace('tomo15Anni');
			$mySession->request_email = $api_params['Email'];
			
			// 入力画面へリダイレクト
			Apm_Util::redirect($this->_config['apm']['ssl']['baseurl'].'inquiry/tomonokai/anniversary15/', $re_params);
			exit();
		}
		
		// トランザクション開始
		$this->_model->startTransaction($db);
		try {
			// DB登録
			$result = $this->_model->insertEntryLog($api_params, $db);
			if ($result == 0) {
				 $this->raiseError('DB登録エラー');
			}
			
			// ユーザ宛メール送信処理
			$mail_info = array();
			$mail_info['from'] = $this->_tomonokai_mail;
			$mail_info['to']   = $api_params['Email'];
			$mail_info['subject']  = '【アパマン友の会】15週連続キャンペーン応募が完了しました。';
			$mail_info['template'] = 'tomonokai/campaign_user.tpl';
			$result = $this->SendMail($mail_info, $api_params, 3);
			if ($result == 0) {
				 $this->raiseError('メール送信エラー');
			}
			
			// 本部宛メール送信処理
			$mail_info = array();
			$mail_info['from'] = $this->_tomonokai_mail;
			$mail_info['to']   = $this->_tomonokai_mail;
			$mail_info['subject']  = '【アパマン友の会】15週連続キャンペーン応募を受付ました。';
			$mail_info['template'] = 'tomonokai/campaign_honbu.tpl';
			$result = $this->SendMail($mail_info, $api_params, 1);
			// 本部メール送信エラーはスルー
			
			// コミット
			__log_debug(__FILE__, __LINE__, __METHOD__, "コミットします");
			$this->_model->commitTransaction($db);
			
			$result = 1;
		} catch (Exception $e) {
			__log_error(__FILE__, __LINE__, __METHOD__, $e->getMessage());
			
			// ロールバック
			__log_debug(__FILE__, __LINE__, __METHOD__, "ロールバックします");
			$this->_model->rollbackTransaction($db);
			
			$result = 0;
		}
		$db->closeConnection();
		
		// リダイレクトパラメータ設定
		$re_params = array();
		$re_params['result'] = $result;
		
		// 送信完了画面へリダイレクト
		Apm_Util::redirect($this->_config['apm']['baseurl'].'inquiry/tomonokai/anniversary15send/', $re_params);
		exit();
	}

	// メール送信
	private function SendMail($mail_info, $addinfo, $mailKbn)
	{
		if ((!isset($mail_info['template']) || empty($mail_info['template']))
		 || (!isset($mail_info['from']) || empty($mail_info['from']))
		 || (!isset($mail_info['to']) || empty($mail_info['to']))){
			// エラー
			return 0;
		}
		
		$strMailFrom = array($mail_info['from'] => "");		//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailTo   = array($mail_info['to'] => "");		//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailCc   = array();
		if (isset($mail_info['cc']) && is_array($mail_info['cc'])) {
			foreach ($mail_info['cc'] as $email) {
				if (!empty($email)) {
					$strMailCc[$email] = '';
				}
			}
		}
		$strMailBcc   = array();
		if (isset($mail_info['bcc']) && is_array($mail_info['bcc'])) {
			foreach ($mail_info['bcc'] as $email) {
				if (!empty($email)) {
					$strMailBcc[$email] = '';
				}
			}
		}
		
		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = $mail_info['subject'];
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		if (count($strMailCc) > 0) {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_CC_KEY] = $strMailCc;
		}
		if (count($strMailBcc) > 0) {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_BCC_KEY] = $strMailBcc;
		}
		
		return $this->sendToiawaseMailJudge($mail_info['template'], $addinfo, $mail_params, $mailKbn);
	}

}
?>
